/**
 * 权限管理
 */
var $ = require("jquery");
var util = require('@yunke/core/util').default;
var _ = require('underscore');
var ui = require('@yunke/core/ui');
var environment = require('@yunke/environment/environment');

var Permission = {
    init: function () {
        this._dataCallbacks = [];
        this._data = null;
        var $body = $('body');

        var self = this;
        self._getData();
    },
    _getData: function () {
        var self = this;
        $.ajax({
            url: '/api/index.php?r=user-auth/get-user-all-auth',
            cache: false,
            success: function (res) {
                if (res['retCode'] == 0) {
                    self._data = res['data'].userAuth;
                    self._update(self._data);
                    self._fire();
                }
            },
            error: function () {
                self._showPermissionError();
            }
        })
    },
    _fire: function () {
        var callback;
        for (var i = 0; callback = this._dataCallbacks[i]; i++) {
            callback(this._data);
        }
        //清空dataCallbacks
        this._dataCallbacks = [];
    },
    /**
     * 获取权限数据
     * @param {function(Object)} callback
     */
    getData: function (callback) {
        this._dataCallbacks.push(callback);

        if (this._data) {
            this._fire();
        }
    },
    /**
     * 提示权限系统错误
     */
    _showPermissionError: function () {
        this.showTipAndForward('对不起，权限系统出现问题', '重新登录', '/login.shtml');
    },
    /**
     * 提示并跳转首页
     * @param {string} content
     * @param {string=} btnText
     * @param {string=} btnUrl
     */
    showTipAndForward: function (content, btnText, btnUrl) {
        btnUrl = btnUrl || '/';
        import("@yunke/art-dialog").then(function (dialog) {
            dialog({
                title: '提示',
                fixed: true,
                content: content,
                width: 350,
                okValue: '返回首页' || btnText,
                cancelDisplay: false,
                ok: function () {
                    location.href = btnUrl;
                    return true;
                },
                cancel: function () {
                    location.href = btnUrl;
                    return true;
                }
            }).showModal();
        });
    },
    /**
     * @param {Array.<string>} data
     */
    _update: function (data) {
        var selBlockArr = [];
        var selInlineArr = [];
        var selInlineBlockArr = [];
        var selTablecellArr = [];
        var selInlineTableArr = [];

        function putClass(clsPart) {
            selBlockArr.push('.p-' + clsPart);
            selBlockArr.push('.p-' + clsPart + '-block');

            selInlineArr.push('.p-' + clsPart + '-inline');
            selInlineBlockArr.push('.p-' + clsPart + '-inline-block');
            selTablecellArr.push('.p-' + clsPart + '-tablecell');
            selInlineTableArr.push('.p-' + clsPart + '-inline-table');
        }

        var item;

        for (var i = 0; item = data[i]; i++) {
            putClass(item);
        }

        $('head').append(
            '<style type="text/css">' +
            '.permission{display:none !important;}' +
            selBlockArr.join(',') + '{display:block !important;}' +
            selInlineArr.join(',') + '{display:inline !important;}' +
            selInlineBlockArr.join(',') + '{display:inline-block !important;}' +
            selTablecellArr.join(',') + '{display:table-cell !important;}' +
            selInlineTableArr.join(',') + '{display:inline-table !important;}' +
            '</style>'
        )
    }
};

Permission.init();

module.exports = Permission;
